<?php
	session_start();
	if(!$_SESSION["username"]){
		//not logged in
		header("location:login.php");
		}
	
	function submitForm(){
		if ($_SERVER['REQUEST_METHOD'] == 'POST'){
			// Connect to server and select databse.
			mysql_connect("localhost", "root", "")or die("cannot connect"); 
			mysql_select_db("Project 5.5")or die("cannot select DB");

			// groupname sent from form 
			$groupname=$_POST['groupname']; 
			$sql="SELECT User_ID FROM users WHERE Username='".$_SESSION["username"]."'";
			$result=mysql_fetch_row(mysql_query($sql));
			$userid=$result[0];
			
			$sql="INSERT INTO groups (Group_Name, Group_Members) VALUES ('".$groupname."', '".$userid."')";
			$result=mysql_query($sql);
			
			$sql="SELECT Group_ID FROM groups WHERE Group_Name='".$groupname."'";
			$result=mysql_fetch_row(mysql_query($sql));
			$groupid=$result[0];
			
			$sql="UPDATE studentinfo SET Group_ID=".$groupid." WHERE User_ID=".$userid;
			$result=mysql_query($sql);
			
			header("location:groups.php");
		}
	}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Group Creation</title>	
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<link href="css/main.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="js/formsValidate.js"></script>
</head>
<body>
	<div id="topbar">
		<div id="qut_logo" />  
		<img src="../img/strapline.png" alt="Slogan" id="slogan"/>
	</div>
	<div id="menubar">
		<div id="breadcrumbs">
			<a href="index.php">Home</a> \ <a href="groups.php">Groups</a> \ <a href="creategroup.php">Create Group</a>
		</div>
		<div id="logout"> 
			<a href="login.php?destroy=true">Log Out</a>
		</div>
	</div>
	
	<div id="body" style="background:#999;padding:0px;">
		<a href="creategroup.php">
			<div id="error">
				<?php submitForm() ?>
			</div>
		</a>
        <div id="namebar_creategroup" ></div>
		<div id="createform"><br />
			<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" name="groupform" style=" border-color: #000; margin-left:30%; margin-right:30%; padding-bottom:10%; padding-top:2%; background-color:#FFF;" onSubmit="return validateForm('groupform');">
				<div style="font-size:17pt; color:#369 ">Enter your Group Name Below</div>
				<br />
			    <input class="form-login" type="text" name="groupname"/>
				<br /><br />
				<input class="CreateBtn" type="submit" value=""/>
				<a href="groups.php">
					<input class="CancelBtn" type="button" name="cancel" value="" />
				</a>
			</form>
		</div>
	</div>
		
	<div id="bottombg">
		<p>
			<a style="color:#333" href="http://www.qut.edu.au/additional/privacy">Privacy</a> 
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/copyright">Copyright</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/disclaimer">Disclaimer</a>
			|
			<a style="color:#333" href="http://www.qut.edu.au/additional/accessibility">Accessibility</a>
		</p>
	</div>
</body>
</html>